/*!
 * \file md_api.hpp
 * \project AI_CTP
 * 
 * \brief CTP行情API封装 - 基于wt_ctp成功实现
 */
#pragma once

#include "ctp_common.hpp"
#include <mutex>
#include <atomic>
#include <thread>
#include <unordered_map>

namespace ctp_api {

// 行情回调接口
class IMdCallback {
public:
    virtual ~IMdCallback() = default;
    
    // 连接事件
    virtual void on_connected() {}
    virtual void on_disconnected(int reason) {}
    virtual void on_login_success() {}
    virtual void on_login_failed(int error_id, const std::string& error_msg) {}
    
    // 数据事件
    virtual void on_tick(const TickInfo& tick) {}
    virtual void on_kline(const BarInfo& kline) {}
    
    // 错误事件
    virtual void on_error(int error_id, const std::string& error_msg) {}
};

// K线合成器类
class KlineBuilder {
public:
    KlineBuilder() = default;
    
    void add_tick(const TickInfo& tick);
    std::vector<BarInfo> get_bars(const std::string& instrument_id, int period) const;
    
private:
    std::unordered_map<std::string, std::unordered_map<int, BarInfo>> bars_;
    mutable std::mutex mutex_;
    
    uint64_t get_bar_time(const std::string& time_str, int period);
    void update_bar(const std::string& instrument_id, int period, const TickInfo& tick);
};

// CTP行情接口封装类
class MdApi : public CThostFtdcMdSpi {
public:
    MdApi();
    virtual ~MdApi();
    
    // 基本操作
    bool init(const std::string& broker_id, const std::string& user_id, 
              const std::string& password, const std::string& front_addr);
    void start();
    void stop();
    bool is_connected() const;
    
    // 订阅操作
    bool subscribe(const std::vector<std::string>& instruments);
    bool unsubscribe(const std::vector<std::string>& instruments);
    
    // K线相关
    void enable_kline(const std::vector<int>& periods = {1, 3, 12, 30});
    void disable_kline();
    
    // 事件回调设置
    void set_callback(std::shared_ptr<IMdCallback> callback);
    
    // K线数据获取
    std::vector<BarInfo> get_klines(const std::string& instrument_id, int period_minutes, int count = 100);
    
    // CTP回调函数重写
    void OnFrontConnected() override;
    void OnFrontDisconnected(int nReason) override;
    void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 
                       CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
    void OnRspSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument,
                           CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
    void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData) override;
    void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;

private:
    // CTP API对象
    CThostFtdcMdApi* md_api_;
    
    // 状态管理
    std::atomic<MdState> state_;
    
    // 连接信息
    std::string broker_id_;
    std::string user_id_;
    std::string password_;
    std::string front_addr_;
    std::string flow_path_;
    
    // 请求ID管理
    std::atomic<int> request_id_;
    
    // 数据转换函数
    TickInfo convert_tick_info(const CThostFtdcDepthMarketDataField* ctp_tick);
    
    // 工具函数
    bool is_error_response(CThostFtdcRspInfoField* pRspInfo);
    int get_next_request_id();
    
    // K线合成器
    std::unique_ptr<KlineBuilder> kline_builder_;
    bool kline_enabled_;
    std::vector<int> kline_periods_;
    bool enable_kline_output_;
    
    // 事件回调
    std::shared_ptr<IMdCallback> callback_;
    
    // 线程安全
    mutable std::mutex mutex_;
    
    // 初始化标志
    bool initialized_;
};

} // namespace ctp_api 